package h0910;

public class PlantFlower {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int[] arr = new int[flowerbed.length + 4];
        arr[0] = 1;
        arr[1] = 0;
        arr[arr.length - 2] = 0;
        arr[arr.length - 1] = 1;
        for (int i = 2; i < arr.length - 2; i++) {
            arr[i] = flowerbed[i - 2];
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            int count = 0;
            while (arr[i] == 0) {
                count++;
                i++;
            }
            sum += (count - 1) / 2;
        }
        return sum >= n;
    }

    public boolean canPlaceFlowers2(int[] flowerbed, int n) {

        int sum = 0;
        for (int i = 0; i < flowerbed.length; i++) {
            if (i == 0 || flowerbed[i - 1] == 0
                    && flowerbed[i] == 0
                    && i == flowerbed.length - 1 || flowerbed[i + 1] == 0) {
                flowerbed[i] = 1; //标记
                sum++;
            }
        }
        return sum >= n;
    }
}
